/*
 * @Author: Zhouhai 497675647@qq.com
 * @Date: 2022-10-08 11:45:40
 * @LastEditors: Zhouhai 497675647@qq.com
 * @LastEditTime: 2022-10-24 17:00:14
 */
import { CustomFormConfig, TRadio } from "@/type.d/formItems";
import { ProFormRadio } from "@ant-design/pro-components";
import { useEffect, useState } from "react";
import request from '@/utils/request'


export const RenderRadio: (itemconfig: CustomFormConfig, disabled: boolean, enumValue?: Record<string, Record<string, any>[]>) => JSX.Element = (itemconfig, disabled, enumValue) => {
    const config = itemconfig.config as TRadio
    const [fieldProps, setFieldProps] = useState({});


    const getEnumValueByKey: (requestUrl?: string) => void = (requestUrl) => {
        if (requestUrl) {
            request({
                url: requestUrl,
                method: 'get'
            }).then((res) => {
                if (res && res.data) {
                    const options = res.data.map((item: any) => {
                        return {
                            label: item.dictLabel,
                            value: item.dictValue
                        }
                    })
                    setFieldProps({options: options})
                }
            })
        } 
    }

    useEffect(() => {
        if(config && config.options){
            setFieldProps({options: config.options})
        }else{
            getEnumValueByKey(config.requestUrl)
        }

    }, [])

    return <ProFormRadio.Group
        tooltip={config?.tooltip}
        key={itemconfig.fieldId}
        fieldId={itemconfig.fieldId}
        name={itemconfig.fieldId}
        label={itemconfig.title}
        colProps={{ md: 8, xl: 6 }}
        fieldProps={fieldProps}
    />
}